<?php

namespace Drupal\shushengyuan\Schemas;

/**
 * @OA\Schema(
 *   schema="Date",
 *   type="object",
 *   description="Date"
 * )
 */
class Date extends AbstractBaseModel {

  protected function getEntityType() {
    return FALSE;
  }

  protected function setValue() {
    [$validity] = $this->context;
    $value = [
      'h' => (string) '00',
      'm' => (string) '00',
      's' => (string) '00',
    ];
    $this->merge($value);
    if ($validity > 0) {
      $hours = ceil($validity / 60 / 60);
      $minutes = ceil($validity / 60 / 60 / 24);
      $seconds = $validity % 60;
      $value = [
        'h' => (string) $this->complementZero($hours),
        'm' => (string) $this->complementZero($minutes),
        's' => (string) $this->complementZero($seconds),
      ];
      $this->merge($value);
    }
  }

  protected function complementZero($v) {
    if ($v < 10) {
      return '0' . $v;
    }
    return $v;
  }
}
